#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
void rar(int [N]);
void show(int [N]);
void insertion_sort(int [N]);
void bubble_sort(int [N]);
void selection_sort(int [N]);
int main(void)
{
    int array[N] = {0};
    // rar(array);
    // printf("新数组\n");
    // show(array);
    // insertion_sort(array);
    // printf("插入排序数组\n");
    // show(array);

    // rar(array);
    // printf("新数组\n");
    // show(array);
    // insertion_sort(array);
    // printf("冒泡排序数组\n");
    // show(array);

    rar(array);
    printf("新数组\n");
    show(array);
    insertion_sort(array);
    printf("选择排序数组\n");
    show(array);


    return 0;
}
void rar(int array[N])
{
    srand(time(0));
    for (int i = 0; i < N; i++)
        array[i] = rand() % 100;
    return;
}
void show(int array[N])
{
    for (int i = 0; i < N; i++)
        printf("%3d", array[i]);
    printf("\n");
    return;
}
void insertion_sort(int array[N])
{
    for (int i = 1; i < N; i++)
    {
        int obj = array[i];
        int pre = i - 1;
        while (pre >= 0 && array[pre] > obj)
        {
            array[pre+1] = array[pre];
            pre--; 
        }
        array[pre+1] = obj;
        // show(array);
    }
    return;
}
void bubble_sort(int array[N])
{
    for (int i = 0; i < N - 1; i++)
        for (int j = 0; j < N - 1 - i; j++)
        {
            if (array[j] > array[j+1])
            {
                int temp = array[j+1];
                array[j+1] = array[j];
                array[j] = temp;
            }
        }

    return;
}
void selection_sort(int array[N])
{
    for (int i = 0; i < N - 1; i++)
    {
        int smallest = i;
        for (int j = i + 1; j < N; j++)
        {
            if (array[j] < array[smallest])
            {
                smallest = j;
            }
        }
        int temp = array[i];
        array[i] = array[i+1];
        array[i+1] = temp;
    }

    return;
}